export function dateTransverter(date:any,times:any){
    // 例 date: 20240908
    // 例 times = ['10:30', '11:00']
    const year = date.substring(0, 4);
    const month = date.substring(4, 6);
    const day = date.substring(6, 8);
    const dateStr = `${year}-${month}-${day}`;


// 定义不同语言环境的日期格式化选项
    const locales = {
        en: { // 英语
            dateFormat:{
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric',
            },
            timeFormat:{
                hour: 'numeric',
                minute: 'numeric',
                hour12: false
            }
        },
        zh: { // 中文
            dateFormat:{
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric',
            },
            timeFormat:{
                hour: 'numeric',
                minute: 'numeric'
            }
        },
        id: { // 印尼语
            dateFormat:{
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric',
            },
            timeFormat:{
                hour: 'numeric',
                minute: 'numeric'
            }
        },
        ar: { // 沙特语（阿拉伯语）
            dateFormat:{
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric',
            },
            timeFormat:{
                hour: 'numeric',
                minute: 'numeric',
                hour12: false
            }
        }
    };
    let lang = JSON.parse(localStorage.getItem('user') as any).lang
    let str : string = ''
    let local : string = ''
    switch (lang) {
        case 'en':
            local='en'
            break;
        case 'cn':
            local='zh'
            break;
        case 'id':
            local='id'
            break;
        case 'sa':
            local='ar'
    }
    const dateFormatter = new Intl.DateTimeFormat(local, locales[local].dateFormat as Object);
    const timeFormatter = new Intl.DateTimeFormat(local, locales[local].timeFormat as Object);
    let formattedTimes = times.map(time => {
        const fullDate = new Date(`${dateStr}T${time}`);
        return timeFormatter.format(fullDate);
    }).join(', ');
    const formattedDate = dateFormatter.format(new Date(dateStr));
    str = `${formattedDate}  ${formattedTimes}`
    console.log(`Formatted Date and Time: ${formattedDate}, ${formattedTimes}`);
    return str
}

export function formatMoney(num:any) {
    // 将数字转换为字符串
    let numStr = num.toString();
    let decimalIndex = numStr.indexOf('.');
    let integerPart = numStr.slice(0, decimalIndex);
    let decimalPart = numStr.slice(decimalIndex + 1);

    // 插入逗号
    integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');

    // 组合整数和小数部分
    return decimalIndex === -1 ? integerPart : `${integerPart}.${decimalPart}`;
}